import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *
from datetime import datetime



def Add(commentContent,votes,appID,customerName):
    app = App.get_by_id(appID)
    user = users.User(customerName)
    datepost = datetime.now()
    comment = AppComment(CommentContent = commentContent,Votes = votes,AppID = app,CustomerName = user,DatePost = datepost) 
    try:
        comment.put()
        return "Complete"
    except:
        return "Error"
        
def GetbyApp(appID,isFull):
    app = App.get_by_id(appID)
    comment_list = []
    if isFull == 1:
        comments = db.GqlQuery("SELECT * FROM AppComment WHERE AppID = :1 ORDER BY DatePost DESC",app)
    else:
        comments = db.GqlQuery("SELECT * FROM AppComment WHERE AppID = :1 ORDER BY DatePost DESC LIMIT 3",app)
    for comment in comments:
        comment_dict = {}
        comment_dict["CommenContent"] = comment.CommentContent
        comment_dict["Votes"] = comment.Votes
        comment_dict["CustomerName"] = comment.CustomerName.nickname()
        comment_dict["DatePost"] = comment.DatePost
        comment_list.append(comment_dict)
    tuple_comment = tuple(comment_list)
    return tuple_comment